keras 切换后端 TensorFlow,cntk,theano
参考
https://keras.io/#configuring-your-keras-backend
Switching from one backend to another
If you have run Keras at least once, you will find the Keras configuration file at:
$HOME/.keras/keras.json
If it isn't there, you can create it.
linux
~/.keras/目录下 keras.json文件 如果不存在可以手动创建
windows
在系统盘中用户(users)当前用户名下的.keras文件夹中,有个keras.json文件。
打开后更改"backend": "xxxxxx"
xxxxx表示需要更换的后端,如"theano"
, "tensorflow"
, or "cntk"
,
NOTE for Windows Users: Please replace $HOME
with %USERPROFILE%
.
The default configuration file looks like this:
{
"image_data_format": "channels_last",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "tensorflow"
}
Simply change the field backend
to "theano"
, "tensorflow"
, or "cntk"
, and Keras will use the new configuration next time you run any Keras code.
You can also define the environment variable KERAS_BACKEND
and this will override what is defined in your config file :
KERAS_BACKEND=tensorflow python -c "from keras import backend"
Using TensorFlow backend.
In Keras it is possible to load more backends than "tensorflow"
, "theano"
, and "cntk"
. Keras can use external backends as well, and this can be performed by changing the keras.json
configuration file, and the "backend"
setting. Suppose you have a Python module called my_module
that you wanted to use as your external backend. The keras.json
configuration file would be changed as follows:
{
"image_data_format": "channels_last",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "my_package.my_module"
}
An external backend must be validated in order to be used, a valid backend must have the following functions: placeholder
, variable
and function
.
If an external backend is not valid due to missing a required entry, an error will be logged notifying which entry/entries are missing.
keras.json details
The keras.json
configuration file contains the following settings:
{
"image_data_format": "channels_last",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "tensorflow"
}
You can change these settings by editing $HOME/.keras/keras.json
.
image_data_format
: String, either"channels_last"
or"channels_first"
. It specifies which data format convention Keras will follow. (keras.backend.image_data_format()
returns it.)- For 2D data (e.g. image),
"channels_last"
assumes(rows, cols, channels)
while"channels_first"
assumes(channels, rows, cols)
. - For 3D data,
"channels_last"
assumes(conv_dim1, conv_dim2, conv_dim3, channels)
while"channels_first"
assumes(channels, conv_dim1, conv_dim2, conv_dim3)
. epsilon
: Float, a numeric fuzzing constant used to avoid dividing by zero in some operations.floatx
: String,"float16"
,"float32"
, or"float64"
. Default float precision.backend
: String,"tensorflow"
,"theano"
, or"cntk"
.